#include "XenMap.h"

bool XenMap::mapOpen = false;

XenMap::XenMap(IDirect3DDevice9 *pIDirect3DDevice9)
{
	this->pIDirect3DDevice9 = pIDirect3DDevice9;
	Initialize();
}

void XenMap::Initialize()
{
	xenMemory = new XenMemory();

	D3DVIEWPORT9 viewport;
	pIDirect3DDevice9->GetViewport(&viewport);

	screenWidth = viewport.Width;
	screenHeight = viewport.Height;

	D3DXCreateTextureFromFile(pIDirect3DDevice9, DSMAP_LOC, &dsMapTexture);
	D3DXCreateTextureFromFile(pIDirect3DDevice9, MAINMAP_LOC, &mainMapTexture);
	D3DXCreateSprite(pIDirect3DDevice9, &mapSprite);

	D3DXCreateTextureFromFile(pIDirect3DDevice9, POINTER_LOC, &pointerTexture);
	D3DXCreateSprite(pIDirect3DDevice9, &pointerSprite);

	mapOpen = false;
}


void XenMap::Render()
{
	if(xenMemory->ReadInChat() != 0 || xenMemory->ReadPage() != 0)
		mapOpen = false;

	if (mapOpen) {
		//Draw the map
		D3DSURFACE_DESC mapDesc;
		dsMapTexture->GetLevelDesc(0, &mapDesc);

		D3DXVECTOR2 mapScale;
		mapScale.x = (float)screenWidth / (float)mapDesc.Width * 0.90;
		mapScale.y = (float)screenHeight / (float)mapDesc.Height * 0.88;

		D3DXVECTOR2 mapPos;
		mapPos.x = ((float)screenWidth * 0.5) - ((float)mapDesc.Width * (float)mapScale.x * 0.5);
		mapPos.y = ((float)screenHeight * 0.5) - ((float)mapDesc.Height * (float)mapScale.y * 0.5);

		D3DXMATRIX mapMatrix;
		D3DXMatrixTransformation2D(&mapMatrix, NULL, 0.0, &mapScale, NULL, 0, &mapPos);

		mapSprite->Begin(D3DXSPRITE_ALPHABLEND);
		mapSprite->SetTransform(&mapMatrix);
		if (xenMemory->ReadServer() == 0) //Main
			mapSprite->Draw(mainMapTexture, NULL, NULL, NULL, D3DCOLOR_ARGB(ConfigFile::MapOpacity, 255, 255, 255));
		if (xenMemory->ReadServer() == 2) //DS
			mapSprite->Draw(dsMapTexture, NULL, NULL, NULL, D3DCOLOR_ARGB(ConfigFile::MapOpacity, 255, 255, 255));
		mapSprite->End();

		//Draw Pointer
		D3DSURFACE_DESC pointerDesc;
		pointerTexture->GetLevelDesc(0, &pointerDesc);

		D3DXVECTOR2 pointerScale;
		pointerScale.x = (float)screenWidth / (float)pointerDesc.Width  * 0.01904762;
		pointerScale.y = (float)screenHeight / (float)pointerDesc.Height * 0.0305;

		int xCoord = xenMemory->ReadLocX();
		int yCoord= xenMemory->ReadLocY();
		float mapTilesWidth = 3210.0;
		float mapTilesHeight = 3210.0;
		float mapWidth = mapDesc.Width * mapScale.x;
		float mapHeight = mapDesc.Height * mapScale.y;
		float pixelsPerTileX = mapWidth / mapTilesWidth;
		float pixelsPerTileY = mapHeight / mapTilesHeight;

		//Position the arrow aligned on the center bottom since arrow points down.
		D3DXVECTOR2 pointerPos;
		pointerPos.x = (float)mapPos.x + ((float)pixelsPerTileX * (float)xCoord) - (float)pointerDesc.Width * pointerScale.x  * 0.5;
		pointerPos.y = (float)mapPos.y + ((float)pixelsPerTileY * (float)yCoord) - (float)pointerDesc.Height * pointerScale.y;

		D3DXMATRIX pointerMatrix;
		D3DXMatrixTransformation2D(&pointerMatrix, NULL, 0.0, &pointerScale, NULL, 0, &pointerPos);

		pointerSprite->Begin(D3DXSPRITE_ALPHABLEND);
		pointerSprite->SetTransform(&pointerMatrix);
		pointerSprite->Draw(pointerTexture, NULL, NULL, NULL, 0xFFFFFFFF);
		pointerSprite->End();
	}
}

void XenMap::ProcessInput()
{
	static bool keyReleased = true;

	if (Input::KeysDown(ConfigFile::MapInput)) {
		if(keyReleased) {
			if (mapOpen) {
				mapOpen = false;
			} else { 
				mapOpen = true;	
			}
		}
		keyReleased = false;
	} else {
		keyReleased = true;
	}
}

void XenMap::Release()
{
	dsMapTexture->Release();
	mainMapTexture->Release();
	pointerTexture->Release();
	pointerSprite->Release();
	mapSprite->Release();

	delete(xenMemory);
}